package com.yy.service.ForegroundService.impl;

import com.yy.service.ForegroundService.IFileuploadService;
import com.yy.utils.OssFileUploadUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FileuploadServiceImpl implements IFileuploadService {

    //绑定文件上传路径到uploadPath
//    @Value("${web.upload-path}")
//    private String uploadPath;

    @Resource
    private OssFileUploadUtil ossFileUploadUtil;
    //本地文件上传
//@Override
//    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
//    public List<String> imgUpload(MultipartFile[] files) {
//        List<String> result = new ArrayList<>();
//        for (MultipartFile uploadFile : files) {
//
//            //创建修改日期路径
//            String format = sdf.format(new Date());
//            File folder = new File(uploadPath + File.separator + format);
//
//            if(!folder.isDirectory()){
//                folder.mkdirs();
//            }
//
//            //时间戳+文件本名作为新文件名
//            String fileName =  System.currentTimeMillis() + "-" + uploadFile.getOriginalFilename();
//            try {
//                uploadFile.transferTo(new File(folder, fileName));
//                //静态资源访问地址返回
//                result.add("http://localhost:8080/"+ format + fileName);
//            } catch (IOException e){
//                System.err.println(e.getMessage());
//            }
//        }
//        return result;
//    }

    //阿里OSS文件上传
    @Override
    public List<String> imgUpload(MultipartFile[] files) {
        List<String> result = new ArrayList<>();
        for (MultipartFile uploadFile : files) {
            String filePath = ossFileUploadUtil.uploadFile(uploadFile);
            if(StringUtils.isNotBlank(filePath)){
                result.add(filePath);
                System.out.println("文件上传成功");
                System.out.printf(filePath);
            }
        }
        return result;
    }
}
